<div class="easyui-layout" data-options="fit:true">
	<div data-options="region:'north'" style="height:40px">
		<div style="float:left;padding:5px 0 0 5px;">
			<a href="javascript:void(0)" onclick="$('#conditionWin3').window('open');" class="easyui-linkbutton" data-options="iconCls:'icon-search'">筛选学生</a>
			<span style="margin-left:20px;">当前筛选条件：</span><span id="currentCondition" style="display:inline-block;vertical-align:sub;color:red;white-space:nowrap;overflow:hidden;text-overflow:ellipsis;width:250px;">全部学生</span>
		</div>
	</div>
    <div data-options="region:'west',split:true" style="width:480px;">
    	<table class="easyui-datagrid" id="studentList3" 
    		data-options="url:'user/students',fit:true,fitColumns:true,rownumbers:true,pagination:true,pageSize:20,pageList:[20,50,100,200,500,1000],
    			toolbar:leftToolbar3,rowStyler:stuRowStyler3,loadFilter:freeStuRender3">
    		<thead>
		        <tr>
		        	<th data-options="field:'ck',checkbox:true"></th>
		            <th data-options="field:'xm',halign:'center',width:100">姓名</th>
		            <th data-options="field:'lqyxsmc',halign:'center',width:110">系名称</th>
		            <th data-options="field:'cwbh',align:'center',width:80">财务编号</th>
		            <th data-options="field:'lqzymc',halign:'center',width:120">主修专业</th>
		        </tr>
		    </thead>
    	</table>
    </div>
    <div data-options="region:'center'">
    	<table class="easyui-datagrid" id="inGroupStudents" data-options="url:'user/groupList',fit:true,pageSize:20,pageList:[20,50,100,200,500,1000],
    		onBeforeLoad:function(param){return param.groupId != undefined;},loadFilter:freeStuRender3,rowStyler:stuRowStyler3,
    		fitColumns:true,rownumbers:true,pagination:true,toolbar:'#groupToolBar'">
    		<thead>
		        <tr>
		        	<th data-options="field:'ck',checkbox:true"></th>
		            <th data-options="field:'xm',width:$(this).width() * 0.3,halign:'center'">姓名</th>
		            <th data-options="field:'lqyxsmc',width:$(this).width() * 0.3,halign:'center'">系名称</th>
		            <th data-options="field:'cwbh',align:'center',width:$(this).width() * 0.2">财务编号</th>
		            <th data-options="field:'lqzymc',width:$(this).width() * 0.2,halign:'center'">主修专业</th>
		        </tr>
		    </thead>
    	</table>
    </div>
</div>

<div id="conditionWin3" class="easyui-window" title="请选择学生的筛选条件" style="width:460px;height:380px;padding-left:30px;padding-top:20px;"
        data-options="iconCls:'icon-save',modal:true,closed:true,minimizable:false,collapsible:false,maximizable:false">
        <table class="form_table" style="border:0px;">
        	<tr><td>系：</td><td><input class="easyui-combobox" id="college3" style="width:240px;" value="-"
            data-options="multiple:true,url:'uni/colleges',valueField:'lqyxsm',textField:'lqyxsmc',onSelect:checkIfAll3,onHidePanel:cascadeMajor3
            	,loadFilter:function(data){return _.union([{id:0,lqyxsm:'-',lqyxsmc: '不限'}],data);}"></td></tr>
            <tr><td>专业：</td><td><input class="easyui-combobox" id="major" style="width:240px;" value="-" 
            data-options="multiple:true,url:'uni/majors',valueField:'lqzydm',textField:'lqzymc',onSelect:checkIfAll3
            	,loadFilter:function(data){return _.union([{id:0,lqzydm:'-',lqzymc: '不限'}],data);}"></td></tr>
            <tr><td>年级：</td><td><input class="easyui-combobox" id="year3" style="width:240px;" value="-" 
            data-options="multiple:true,url:'grant/years',valueField:'id',textField:'value',panelHeight:110,onSelect:checkIfAll3
            	,loadFilter:function(data){return _.union([{id:'-',value: '不限'}],data);}"></td></tr>
            <tr><td>类别：</td><td><input class="easyui-combobox" id="classify3" style="width:240px;" value="-" 
            data-options="multiple:true,url:'grant/classify',valueField:'id',textField:'value',panelHeight:110,onSelect:checkIfAll3
            	,loadFilter:function(data){return _.union([{id:'-',value: '不限'}],data);}"></td></tr>
            <tr><td></td><td><a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search'" onclick="refreshStudents3()">确定</a>
            <a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-no'" onclick="$('#conditionWin3').window('close');">取消</a></td></tr>
        </table>
</div>

<div id="groupToolBar">
	<table cellspacing="0" cellpadding="0">
		<tbody><tr>
			<td><a href="javascript:void(0)" class="easyui-linkbutton" onclick="doDelGroupList()" data-options="iconCls:'icon-remove',plain:true">剔除</a></td>
			<td><div class="datagrid-btn-separator"></div></td>
			<td><a href="javascript:void(0)" class="easyui-linkbutton" onclick="$('#switchGroupWin').window('open');" data-options="iconCls:'icon-cut',plain:true">名单设置</a></td>
			<td><div class="datagrid-btn-separator"></div></td>
			<td>当前名单：<span id="groupName" style="color:green;font-weight:bold;">测试名单</span><input type="hidden" id="groupId" value="1"></td>
		</tr></tbody>
	</table>
</div>

<div id="switchGroupWin" class="easyui-window" title="名单设置" style="width:340px;height:180px"
	data-options="iconCls:'icon-save',modal:true,closed:true,minimizable:false,collapsible:false,maximizable:false">
	<table class="form_table" style="border:0px;">
		<tr><td></td><td></td></tr>
		<tr><td></td><td></td></tr>
		<tr><td>选择名单：</td><td><select class="select" id="groupList" style="width:196px;"></select></td></tr>
		<tr><td></td><td>
			<input type="button" value="新建" class="btnSubmit" onclick="createGroup()"></input>
			<input type="button" value="删除" class="btnSubmit" onclick="delGroup()"></input>
			<input type="button" value="设为当前" class="btnSubmit" onclick="markAsCurrentGroup()"></input></td></tr>
	</table>
</div>

<script>

$(function(){
	loadGroups();
});

function loadGroups(callback,groupId,groupName) {//刷新页面中所选名单与学生列表信息
	$.ajax({
		url:'user/group',
		success:function(result) {
			if(result == null || result.length < 1) {
				$.messager.alert('提示','可选名单为空，请点击<font color=red>名单设置</font>新建名单！');return;
			}
			$('#groupId').val(groupId != undefined ? groupId : result[0].id);
			$('#groupName').html(groupName != undefined? groupName : result[0].name);
			$('#groupList').prop('data',result);
			$('#groupList').html($.map(result,function(row){return '<option value="' + row.id + '">' + row.name + '</option>';}).join(''));
			if(groupId) {$('#groupList').val(groupId)};
			
			$('#inGroupStudents').datagrid('load',{groupId:$('#groupId').val()});
			if(callback) callback();
		}
	});
}

var leftToolbar3 = [{
    text:'保存至名单',
    iconCls:'icon-add',
    handler:function(){
		if($('#studentList3').datagrid('getChecked').length == 0) {
			$.messager.alert('提示','您尚未选择任何学生！');return;
		}
		var confirmInfo = '确认要将所选学生保存至名单"<font color=red>' + $('#groupName').html() 
				+ '</font>"中么？<br/>若想切换名单，请点击右侧的<font color=red>名单设置</font>！';
		$.messager.confirm('提示',confirmInfo,function(r) {
			if(r) {
				var xhs = $.map($('#studentList3').datagrid('getChecked'),function(row){return row.xh;});
				doSaveGroupList(xhs);
				$('#inGroupStudents').datagrid('reload');
			}
		});
    }
}];

function checkIfAll3(record) {
	if(record[$(this).combobox('options').valueField] == '-') {
		$(this).combobox('setValue','-');
	} else {
		$(this).combobox('unselect','-');
	}
}

function cascadeMajor3() {
	var college = $('#college3').combobox('getValues').join(',');
	$('#major3').combobox('reload','uni/majors' + (college == '-'? '':'?college=' + college));
}

function refreshStudents3() {
	var info = '学院:' + $('#college3').combobox('getText') + ',' +
			'专业:' + $('#major3').combobox('getText') + ',' +
			'年级:' + $('#year3').combobox('getText') + ',' +
			'类别:' + $('#classify3').combobox('getText') + '';
	$('#currentCondition').html(info);
	$('#currentCondition').attr('title',info);

	$('#studentList3').datagrid('reload',
		{college:$('#college3').combobox('getValues').join(','),
		major:$('#major3').combobox('getValues').join(','),
		year:$('#year3').combobox('getValues').join(','),
		classify:$('#classify3').combobox('getValues').join(',')});

	$('#conditionWin3').window('close');
}

function stuRowStyler3(index,row) {
	if(row.freeze) {return 'background-color:#F0F0F0;color:#979797;';}
}

function freeStuRender3(data) {
	var freezeList = $.parseJSON($.ajax({url: "user/freezeList",async: false}).responseText);
	var freezeXhs = $.map(freezeList,function(row){return row.xh;});
	$.each(data.rows,function(id,row) {//标记冻结学生
		if($.inArray(row.xh,freezeXhs) > -1) {row.freeze = true;row.xm = row.xm + '[已冻结]';}
	});
	return data;
}

function doSaveGroupList(xhs) {
	$.ajax({
		url:'user/groupListAct',
		data:'action=add&xhs=' + xhs + '&groupId=' + $('#groupId').val(),
		success:function(result) {
			if(result.errorInfo != 'none') {$.messager.alert('提示',result.errorInfo);return;}
			$.messager.alert('提示','添加名单成功！');
			loadGroups(undefined,$('#groupId').val(),$('#groupName').html());
		}
	});
}

function createGroup() {
	$.messager.prompt('新建名单', '请输入新建名单的名称', function(r){
        if(r != undefined && $.trim(r) == '') {$.messager.alert('提示','名单不能为空！');return;}
        $.ajax({
			url:'user/groupAct',
			data:'action=add&groupName=' + $.trim(r),
			success:function(result) {
				if(result.errorInfo != 'none') {$.messager.alert('提示',result.errorInfo);return;}
				
				$.messager.alert('提示','创建名单成功！');
				loadGroups(function(){
					$.each($('#groupList').prop('data'),function(id,row){if(row.name == $.trim(r))$('#groupList').val(row.id);});
				});
			}
        });
    });
}

function delGroup() {
	var group = $('#groupList :selected');
	$.messager.confirm('提示','确定要删除名单"<font color=red>' + group.html() + '</font>？',function(r) {
		if(!r) return;
		$.ajax({
			url:'user/groupAct',
			data:'action=del&groupId=' + group.val(),
			success:function(result) {
				if(result.errorInfo != 'none') {$.messager.alert('提示',result.errorInfo);return;}
				$.messager.alert('提示','删除名单成功！');
				if(group.val() == $('#groupId').val()) {//删除当前展示名单时需要重新加载名单信息
					loadGroups();return;
				}

				$.ajax({
					url:'user/group',
					success:function(result) {
						$('#groupList').prop('data',result);
						$('#groupList').html($.map(result,function(row){return '<option value="' + row.id + '">' + row.name + '</option>';}).join(''));
					}
				});
			}
		});
	});
}

function markAsCurrentGroup() {
	$('#groupId').val($('#groupList').val());
	$('#groupName').html($('#groupList :selected').html());
	loadGroups(undefined,$('#groupId').val(),$('#groupName').html());
	$.messager.alert('提示','设置成功！');
}

function doDelGroupList() {
	if($('#inGroupStudents').datagrid('getChecked').length == 0) {
		$.messager.alert('提示','您尚未选择任何学生！');return;
	}
	$.messager.confirm('提示','确定要剔除所选学生么？',function(r) {
		if(!r) return;
		var xhs = $.map($('#inGroupStudents').datagrid('getChecked'),function(row){return row.xh;});
		$.ajax({
			url:'user/groupListAct',
			data:'action=del&xhs=' + xhs + '&groupId=' + $('#groupId').val(),
			success:function(result) {
				if(result.errorInfo != 'none') {$.messager.alert('提示',result.errorInfo);return;}
				$.messager.alert('提示','剔除学生成功！');
				$('#inGroupStudents').datagrid('reload');
			}
		});
	});
}
</script>